package com.cnswhy.cloud.server.comsumer.consumer;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.common.redis.annotation.RedisLock;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.mq.SpellFinishedMsg;
import com.cnswhy.cloud.server.common.entity.mq.SpellInfoMsg;
import com.cnswhy.cloud.server.common.entity.mq.SpellMsg;
import com.cnswhy.cloud.server.comsumer.event.SpellInfoEvent;
import com.cnswhy.cloud.server.core.entity.SpellInfo;
import com.cnswhy.cloud.server.core.entity.SpellOrder;
import com.cnswhy.cloud.server.core.entity.SpellPrice;
import com.cnswhy.cloud.server.core.service.CustomerWalletService;
import com.cnswhy.cloud.server.core.service.SpellInfoService;
import com.cnswhy.cloud.server.core.service.spell.SpellOrderService;
import com.cnswhy.cloud.server.core.service.spell.SpellPriceService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.cnswhy.cloud.common.core.context.BaseContextConstants.LOG_TRACE_ID;

@Component
@Slf4j
public class SpellInfoConsumer implements SpellInfoEvent {
    @Autowired
    CustomerWalletService customerWalletService;
    @Autowired
    SpellOrderService spellOrderService;
    @Autowired
    SpellPriceService spellPriceService;
    @Autowired
    SpellInfoService spellInfoService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    MessageService messageService;

    @Override
    @RedisLock(name = "spellInfo")
    public void update(SpellInfoMsg spellInfoMsg) {
        log.info("更新拼团信息:{}",JSON.toJSONString(spellInfoMsg));
        SpellInfo spellInfo = spellInfoService.getOne(Wraps.<SpellInfo>lbQ().eq(SpellInfo::getOrderNum, spellInfoMsg.getSpellOrderId()));
        if(spellInfo != null){
            if(spellInfoMsg.getLoveVlue() != null) {
                spellInfo.setLoveVlue(spellInfo.getLoveVlue() + spellInfoMsg.getLoveVlue());
            }
            if(spellInfoMsg.getSpellValue() != null) {
                spellInfo.setSpellValue(spellInfo.getSpellValue() + spellInfoMsg.getSpellValue());
            }
            if(spellInfoMsg.getContributionValue() != null) {
                spellInfo.setContributionValue(spellInfo.getContributionValue() + spellInfoMsg.getContributionValue());
            }
            if(spellInfoMsg.getShareValue() != null) {
                spellInfo.setShareValue(spellInfo.getShareValue() + spellInfoMsg.getShareValue());
            }
            spellInfoService.updateById(spellInfo);
        }
    }

}
